/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.animation;


import ohos.aafwk.ability.Ability;
import ohos.agp.animation.*;
import ohos.agp.components.Component;

public class AnimationOrAnimator {
    public AnimatorProperty animator;
    public AnimatorGroup animatorGroup;
    private OneShotEndAction mEndAction;

    public static AnimationOrAnimator loadAnimation(Ability ability, int nextAnim) {
        Animator animator = AnimatorScatter.getInstance(ability).parse(nextAnim);
        if (animator != null) {
            return new AnimationOrAnimator(animator);
        }
        throw new IllegalArgumentException("resource is error");
    }

    public static AnimationOrAnimator loadAnimation(Ability ability, Animator nextAnim) {
        if (nextAnim instanceof AnimatorGroup) {
            return new AnimationOrAnimator((AnimatorGroup) nextAnim);
        } else {
            return new AnimationOrAnimator(nextAnim);
        }
    }

    public AnimationOrAnimator(Animator animator) {
        this.animator = (AnimatorProperty) animator;
        this.animatorGroup = null;
        if (animator == null) {
            throw new IllegalStateException("Animator cannot be null");
        }
    }

    public AnimationOrAnimator(AnimatorGroup animator) {
        this.animatorGroup = animator;
        this.animator = null;
        if (animator == null) {
            throw new IllegalStateException("Animator cannot be null");
        }
    }


    private static class OneShotEndAction {
        private final Runnable runnable;
        private boolean executed = false;

        private OneShotEndAction(Runnable runnable) {
            if (runnable == null) {
                throw new IllegalStateException("runnable cannot be null");
            }
            this.runnable = runnable;
        }

        public void run() {
            if (executed) {
                return;
            }
            executed = true;
            runnable.run();
        }
    }

    public void addEndAction(final Runnable runnable) {
        mEndAction = new OneShotEndAction(runnable);
        if (animator != null) {
            animator.setStateChangedListener(new StateChangedListener() {
                @Override
                public void onEnd(Animator animator) {
                    mEndAction.run();
                    ((AnimatorProperty) animator).setStateChangedListener(null);
                    AnimationOrAnimator.this.animator = null;
                }
            });
        } else if (animatorGroup != null) {
            animatorGroup.setStateChangedListener(new StateChangedListener() {
                @Override
                public void onEnd(Animator animator) {
                    mEndAction.run();
                    ((AnimatorGroup) animator).setStateChangedListener(null);
                    AnimationOrAnimator.this.animatorGroup = null;
                }
            });

        }

    }

    private class StateChangedListener implements Animator.StateChangedListener {
        @Override
        public void onStart(Animator animator) {

        }

        @Override
        public void onStop(Animator animator) {

        }

        @Override
        public void onCancel(Animator animator) {

        }

        @Override
        public void onEnd(Animator animator) {

        }

        @Override
        public void onPause(Animator animator) {

        }

        @Override
        public void onResume(Animator animator) {

        }
    }


    public void start(Component component) {
        if (animator != null) {
            animator.setTarget(component);
            animator.start();
        } else if (animatorGroup != null) {
            animatorGroup.start();
        }
    }

    public void end() {
        if (animator != null) {
            animator.end();
        } else if (animatorGroup != null) {
            animatorGroup.end();
        }
    }
}
